package com.dbpediamobile;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;

import android.R.color;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.EditText;
import android.widget.Toast;

//clase que "dibuja" el grafo
public class GrafoView extends View{
	
	private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	Paint white = new Paint();
	//se crean objetos visuales
	private RectF ovalA, ovalB, ovalC, ovalD, ovalE, ovalF, ovalG, ovalH, ovalI;
	private RectF ovalAA, ovalBB, ovalCC, ovalDD, ovalEE, ovalFF,ovalGG; 
	String[] nodos = new String[8];
	int offset=0;
	private static int limit=8;
	int pagina=0;//numero de pagina de vistas del grafo
	String input="", nextInput,prevInput="";	
	String newInput="";
	private Animation anim;//variable para la animacion
	
	public GrafoView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
        mPaint.setColor(Color.BLACK);
        mPaint.setAntiAlias(true);

	    //se crea un nuevo objeto pain para tener uno de color blanco
	    
	    white.setColor(Color.WHITE);
	    mPaint.setAntiAlias(true);
	    white.setAntiAlias(true);
	    BusquedaAvanzada ba = new BusquedaAvanzada();
	    input = ba.txtInput.getText().toString();
	    
	    //input="talca";
	    consultar(input, offset,limit);
	    
	}
	public String getInput(){
		return this.input;
	}
	//busca sobre el nodo seleccionado
	public boolean goNodo(){
		offset=0;
		int result = consultar(newInput, (offset),limit);
		if(result>1){
			prevInput = input;
			input = newInput;
			
			this.invalidate();
			return true;
		}
	    
		return false;
	}
	//proximos resultados de la busqueda
	public boolean next(){
		int result = consultar(input, (++offset*8),limit);
		if(result>1){
			
			this.invalidate();
			return true;
		}
		offset--;
	    
		return false;
	}
	//resultados anteriores de la busqueda
	public boolean prev(){
		if(offset>0){
			offset--;
			int result = consultar(input, (offset*8),limit);
			this.invalidate();
			return true;			
		}
		else{
			return false;
		}
	}
	//nodo anterior
	public boolean back(){
		if(prevInput!=input && prevInput!=""){
			offset=0;
			int result = consultar(prevInput, (offset),limit);
			input = prevInput;
			this.invalidate();
			newInput="";
			return true;
		}
	    
		return false;
	}
	private void createAnim(Canvas canvas) {
        anim = new RotateAnimation(0, 360, 10, 10);
        //anim.setRepeatMode(Animation.REVERSE);
        anim.setRepeatCount(1);
        anim.setDuration(3000L);
        anim.setInterpolator(new AccelerateDecelerateInterpolator());

        startAnimation(anim);
    }
	
	//dibuja la vista
	protected void onDraw(Canvas canvas) {
	    super.onDraw(canvas);
	    // creates the animation the first time
        //if (anim == null) {
            //createAnim(canvas);
        //}
        
	    float width = canvas.getWidth();
	    float height = canvas.getHeight();
	    //this.setBackgroundColor(Color.WHITE);
	    //crea los 9 ovalos negros rellenos que representan el grafo
	    ovalA = new RectF((float)(width/3.0/2.0), (float)(height/7.0), (float)(width/2.0), (float)(height*(2.0/7.0)));
	    ovalB = new RectF((float)(width/2.0), (float)(height/7.0), (float)(width-width/6.0), (float)(height*(2.0/7.0)));
	    ovalC = new RectF(0, (float)(height*(2.0/7.0)), (float)(width/3.0), (float)(height*(3.0/7.0)));
	    ovalD = new RectF((float)(width*(2.0/3.0)), (float)(height*(2.0/7.0)), width, (float)(height*(3.0/7.0)));
	    ovalE = new RectF((float)(width/3.0), (float)(height*(3.0/7.0)), (float)(width*(2.0/3.0)), (float)(height*(4.0/7.0)));
	    ovalF = new RectF(0, (float)(height*(4.0/7.0)), (float)(width/3.0), (float)(height*(5.0/7.0)));
	    ovalG = new RectF((float)(width*(2.0/3.0)), (float)(height*(4.0/7.0)), width, (float)(height*(5.0/7.0)));
	    ovalH = new RectF((float)(width/3.0/2.0), (float)(height*(5.0/7.0)), (float)(width/2.0), (float)(height*(6.0/7.0)));
	    ovalI = new RectF((float)(width/2.0), (float)(height*(5.0/7.0)), (float)(width-width/6.0), (float)(height*(6.0/7.0)));
	   
	    //crea ovalos blancos 
	    RectF ovalAA = new RectF((float)(width/3.0/2.0)+3, (float)(height/7.0)+3, (float)(width/2.0)-3, (float)(height*(2.0/7.0))-3);
	    RectF ovalBB = new RectF((float)(width/2.0)+3, (float)(height/7.0)+3, (float)(width-width/6.0)-3, (float)(height*(2.0/7.0))-3);
	    RectF ovalCC = new RectF(0+3, (float)(height*(2.0/7.0))+3, (float)(width/3.0)-3, (float)(height*(3.0/7.0))-3);
	    RectF ovalDD = new RectF((float)(width*(2.0/3.0))+3, (float)(height*(2.0/7.0))+3, width-3, (float)(height*(3.0/7.0))-3);
	    RectF ovalEE = new RectF((float)(width/3.0)+3, (float)(height*(3.0/7.0))+3, (float)(width*(2.0/3.0))-3, (float)(height*(4.0/7.0))-3);
	    RectF ovalFF = new RectF(0+3, (float)(height*(4.0/7.0))+3, (float)(width/3.0)-3, (float)(height*(5.0/7.0))-3);
	    RectF ovalGG = new RectF((float)(width*(2.0/3.0))+3, (float)(height*(4.0/7.0))+3, width-3, (float)(height*(5.0/7.0))-3);
	    RectF ovalHH = new RectF((float)(width/3.0/2.0)+3, (float)(height*(5.0/7.0))+3, (float)(width/2.0)-3, (float)(height*(6.0/7.0))-3);
	    RectF ovalII = new RectF((float)(width/2.0)+3, (float)(height*(5.0/7.0))+3, (float)(width-width/6.0)-3, (float)(height*(6.0/7.0))-3);
	   
	    //dibuja lineas para conectar los grafos
	    
	    canvas.drawLine(ovalA.centerX(), ovalA.centerY(), ovalE.centerX(), ovalE.centerY(), mPaint);
	    canvas.drawLine(ovalB.centerX(), ovalB.centerY(), ovalE.centerX(), ovalE.centerY(), mPaint);
	    canvas.drawLine(ovalC.centerX(), ovalC.centerY(), ovalE.centerX(), ovalE.centerY(), mPaint);
	    canvas.drawLine(ovalD.centerX(), ovalD.centerY(), ovalE.centerX(), ovalE.centerY(), mPaint);
	    canvas.drawLine(ovalE.centerX(), ovalE.centerY(), ovalE.centerX(), ovalE.centerY(), mPaint);
	    canvas.drawLine(ovalF.centerX(), ovalF.centerY(), ovalE.centerX(), ovalE.centerY(), mPaint);
	    canvas.drawLine(ovalG.centerX(), ovalG.centerY(), ovalE.centerX(), ovalE.centerY(), mPaint);
	    canvas.drawLine(ovalH.centerX(), ovalH.centerY(), ovalE.centerX(), ovalE.centerY(), mPaint);
	    canvas.drawLine(ovalI.centerX(), ovalI.centerY(), ovalE.centerX(), ovalE.centerY(), mPaint);
	    //dibuja los ovalos creados anteriormente
	    canvas.drawOval(ovalA, mPaint);
	    canvas.drawOval(ovalB, mPaint);
	    canvas.drawOval(ovalC, mPaint);
	    canvas.drawOval(ovalD, mPaint);
	    canvas.drawOval(ovalE, mPaint);
	    canvas.drawOval(ovalF, mPaint);
	    canvas.drawOval(ovalG, mPaint);
	    canvas.drawOval(ovalH, mPaint);
	    canvas.drawOval(ovalI, mPaint);
	    
	    canvas.drawOval(ovalAA, white);
	    canvas.drawOval(ovalBB, white);
	    canvas.drawOval(ovalCC, white);
	    canvas.drawOval(ovalDD, white);
	    canvas.drawOval(ovalEE, white);
	    canvas.drawOval(ovalFF, white);
	    canvas.drawOval(ovalGG, white);
	    canvas.drawOval(ovalHH, white);
	    canvas.drawOval(ovalII, white);
	    
	    
	    
	    
		ArrayList<RectF> a=new ArrayList();
		a.add(ovalA);
		a.add(ovalB);
		a.add(ovalC);
		a.add(ovalD);
		a.add(ovalF);
		a.add(ovalG);
		a.add(ovalH);
		a.add(ovalI);
		//dibuja cada texto de los nodos
		canvas.drawText(input, ovalE.centerX()-3*input.length(), ovalE.centerY(), mPaint);
		if(nodos!=null){
			for(int i=0;i<nodos.length;i++){
				if(nodos[i].length()>15){
					float Y=a.get(i).centerY();
					String[] cachito = nodos[i].split(" ");
					for(int j=0;j<cachito.length;j++){
						if(cachito.length>2){
							float x=(float) (a.get(i).centerX()-5*cachito[j].length());
							canvas.drawText(cachito[j], x, Y+(j*6), mPaint);
							j++;
							if(j!=cachito.length)
								canvas.drawText(cachito[j], a.get(i).centerX()+5, Y+((j-1)*6), mPaint);
						}
						else
							canvas.drawText(cachito[j], (float) (a.get(i).centerX()-2.5*cachito[j].length()), Y+(j*12), mPaint);
					}
				}
				else{
					canvas.drawText(nodos[i], (float) (a.get(i).centerX()-2.5*nodos[i].length()), a.get(i).centerY(), mPaint);
				}
			}
		}
		
	}
	private int consultar(String input, int offset, int limit){
		//se obtienen los nodos a mostrar	
		int numNodos=0;
		try {
			GrafoConexion gf = new GrafoConexion(input, offset, limit );
			String s = gf.Conectar();
			String[] temp = s.split("\\*");
			if(temp.length>1)
				nodos = s.split("\\*");
			
			//Toast.makeText(getContext(),nodos[0], Toast.LENGTH_LONG).show();
			//+" "+nodos[1]+" "+nodos[2]+" "+nodos[3]+" "+nodos[4]+" "+nodos[5]+" "+nodos[6]+" "+nodos[7]
			numNodos = temp.length;
			
		} catch (IOException e) {
			Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
			//e.printStackTrace();
		}
		return numNodos;
	}
	public boolean onTouchEvent (MotionEvent event){
		//Toast.makeText(getContext(), "X "+event.getX()+" Y "+event.getY(), Toast.LENGTH_LONG).show();
		float x = event.getX();
		float y = event.getY();
		int numnodos = nodos.length;
		if(event.getAction() == 0){
			if(ovalA.contains(x, y) && numnodos>0)
				newInput = nodos[0];
			else if(ovalB.contains(x, y) && numnodos>1)
				newInput = nodos[1];
			else if(ovalC.contains(x, y) && numnodos>2)
				newInput = nodos[2];
			else if(ovalD.contains(x, y) && numnodos>3)
				newInput = nodos[3];
			else if(ovalF.contains(x, y) && numnodos>4)
				newInput = nodos[4];
			else if(ovalG.contains(x, y) && numnodos>5)
				newInput = nodos[5];
			else if(ovalH.contains(x, y) && numnodos>6)
				newInput = nodos[6];
			else if(ovalI.contains(x, y) && numnodos>7)
				newInput = nodos[7];
			else{
				newInput = "";
			}
			if(newInput.compareTo("")!=0){
				newInput=newInput.replace(" ", "+");
				
			if(newInput.compareTo("")!=0 && !this.goNodo())
				Toast.makeText(getContext(), "No results found", Toast.LENGTH_LONG).show();
			}
			newInput="";
		}
		return true;
		
	}
}
